package zld369504.tw.m_team.fragments;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.content.LocalBroadcastManager;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import zld369504.tw.m_team.BookmarksActivity;
import zld369504.tw.m_team.ControlPanelActivity;
import zld369504.tw.m_team.R;
import zld369504.tw.m_team.dao.User;

public class SlideMenuFragment extends Fragment implements NavigationView.OnNavigationItemSelectedListener {

    private View rootView;
    private NavigationView navigationView;

    private TextView tvUserName;
    private TextView tvDownloadCount;
    private TextView tvUploadCount;
    private TextView tvSharePoint;
    private TextView tvMagicPoint;

    public static SlideMenuFragment newInstance() {
        SlideMenuFragment fragment = new SlideMenuFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_slidemenu, container, false);
        navigationView = rootView.findViewById(R.id.navigationView);
        navigationView.setNavigationItemSelectedListener(this);
        View headerView = navigationView.getHeaderView(0);

        tvUserName = (TextView) headerView.findViewById(R.id.tvUserName);
        tvUploadCount = (TextView) headerView.findViewById(R.id.tvUploadCount);
        tvDownloadCount = (TextView) headerView.findViewById(R.id.tvDownloadCount);
        tvSharePoint = (TextView) headerView.findViewById(R.id.tvSharePoint);
        tvMagicPoint = (TextView) headerView.findViewById(R.id.tvMagicPoint);
        return rootView;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        User user = User.getInstance();
        tvUserName.setText(user.getUserName());
        tvDownloadCount.setText(user.getDownloadCount());
        tvUploadCount.setText(user.getUploadCount());
        tvMagicPoint.setText(user.getMagicCount());
        tvSharePoint.setText(user.getSharePoint());
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.menu_control_panel) {
            startActivity(new Intent(getActivity(), ControlPanelActivity.class));
            return true;
        }
        if (item.getItemId() == R.id.menu_bookmarks) {
            startActivity(new Intent(getActivity(), BookmarksActivity.class));
            return true;
        }
        LocalBroadcastManager mgr = LocalBroadcastManager.getInstance(getActivity());
        Intent intent = new Intent("tw.zld369504.m-team.MainNavigationViewItemSelected");
        switch (item.getItemId()) {
            case R.id.menu_adult:
                intent.putExtra("fragmentTag", TorrentListFragment.adult);
                break;
            case R.id.menu_comprehensive:
                intent.putExtra("fragmentTag", TorrentListFragment.comprehensive);
                break;
            case R.id.menu_four_k:
                intent.putExtra("fragmentTag", TorrentListFragment.fourK);
                break;
            case R.id.menu_home:
                intent.putExtra("fragmentTag", MainFragment.TAG);
                break;
        }
        intent.putExtra("menu_id", item.getItemId());
        intent.putExtra("menu_title", item.getTitle());
        mgr.sendBroadcast(intent);
        return true;
    }
}
